﻿using System;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;

namespace RxjhServer
{
    public class Aes
    {
        public const string Key1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456";
        public const string Key2 = "abcdefghijklmnopqrstuvwxyz678910";
        public static string Encrypt(string key, string code)
        {
            string original = GetSerialNumber(code);
            using (AesManaged myAes = new AesManaged())
            {
                byte[] bytes = Encoding.ASCII.GetBytes(key);
                myAes.Key = bytes;
                myAes.IV = bytes.Take(16).ToArray();
                byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
                return Convert.ToBase64String(encrypted);
            }
        }

        public static string Decrypt(string key, string code)
        {
            try
            {
                using (AesManaged myAes = new AesManaged())
                {
                    byte[] bytes = Encoding.ASCII.GetBytes(key);
                    myAes.Key = bytes;
                    myAes.IV = bytes.Take(16).ToArray();
                    byte[] encrypted = Convert.FromBase64String(code);
                    string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
                    return roundtrip;
                }
            }
            catch { return null; }
        }

        public static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
        {
            if (plainText == null || plainText.Length <= 0) return null;
            if (Key == null || Key.Length <= 0) return null;
            if (IV == null || IV.Length <= 0) return null;
            byte[] encrypted;
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }
            return encrypted;

        }

        public static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            if (cipherText == null || cipherText.Length <= 0) return null;
            if (Key == null || Key.Length <= 0) return null;
            if (IV == null || IV.Length <= 0) return null;
            string plaintext = null;
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }

        public static string GetSerialNumber(string code)
        {
            string number = "";
            while (number.Length < 32)
            {
                for (int i = 0; i < code.Length; i++)
                {
                    number += code[i];
                }
            }
            return number;
        }

        public static string GetMachineCode()
        {
            return GetCpuId() + GetDiskNo();
        }

        public static string GetDiskNo()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            disk.Get();
            return disk.GetPropertyValue("VolumeSerialNumber").ToString();
        }


        public static string GetCpuId()
        {
            string strCpu = null;
            ManagementClass myCpu = new ManagementClass("win32_Processor");
            ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
            foreach (ManagementObject myObject in myCpuConnection)
            {
                strCpu = myObject.Properties["Processorid"].Value.ToString();
                break;
            }
            return strCpu;
        }
    }
}
